using System;
using System.Collections.Generic;
using System.Text;

namespace Elte.SqlSpIdx
{
    public class HalfspaceList : List<Halfspace>
    {
        private int dimensions;

        public HalfspaceList() : base()
        {
        }

        public HalfspaceList(int dim) : base()
        {
            dimensions = dim;
        }

        public HalfspaceList(double[][] a, double[] b)
            : base()
        {
            dimensions = a[0].Length;

            for (int i = 0; i < b.Length; i ++)
                base.Add(new Halfspace(a[i], b[i]));
        }

        public int Dimensions
        {
            get { return dimensions; }
        }

        public double[][] GetAMatrix()
        {
            double[][] res;

            res = new double[base.Count][];
            for (int i = 0; i < base.Count; i++)
            {
                res[i] = new double[dimensions];
                for (int j = 0; j < dimensions; j++)
                {
                    res[i][j] = base[i].A[j];
                }
            }

            return res;
        }

        public double[] GetBVector()
        {
            double[] res;

            res = new double[base.Count];
            for (int i = 0; i < base.Count; i++)
                res[i] = base[i].B;

            return res;
        }

        public double[][] GetInhomogenMatrix()
        {
            double[][] res;

            res = new double[base.Count][];
            for (int i = 0; i < base.Count; i++)
            {
                res[i] = new double[dimensions + 1];
                res[i][0] = base[i].B;
                for (int j = 0; j < dimensions; j++)
                {
                    res[i][j + 1] = base[i].A[j];
                }
            }

            return res;
        }

        public bool IsInside(double[] v)
        {
            foreach (Halfspace l in this)
            {
                if (!l.IsInside(v))
                    return false;
            }
            return true;
        }

        public Halfspace.InsideType IsInsideBox(double[] Box)
        {
            bool partly = false;
            Halfspace.InsideType result;
            foreach (Halfspace l in this)
            {
                result = l.IsInsideBox(Box);
                if (result== Halfspace.InsideType.None) return Halfspace.InsideType.None;
                if (result == Halfspace.InsideType.Part) partly = true;
            }
            if (partly) return Halfspace.InsideType.Part;
            return Halfspace.InsideType.All;
        }
    }
}
